<?php
class M_Search extends Apps_Db_Table_Abstract {
    public function getResult($keyword) {
        $listTour   = $this->listSearchTour($keyword)->query()->fetchAll();
        $listCruise = $this->listSearchCruise($keyword)->query()->fetchAll();

        return array_merge($listTour, $listCruise);
    }

    public function listSearchTour($keyword) {
        $q = $this->_db
                    ->select()
                    ->from('tour', null)
                    ->join('article', 'article.reference_id = tour.id', null)
                    ->where('article.reference_type =? ', M_DbTable_Article::REFERENCE_TYPE_TOUR)
                    ->where('tour.status =? ', 1)
                    ->where('article.title LIKE "%'. $keyword .'%" OR article.en_title LIKE "%'. $keyword .'%"')
                    ->order('tour.sorting_number ASC')
                    ->columns(array('id'                  => 'tour.id',
                                    'thumbnail'           => 'article.thumbnail'));

        if($this->getLang() == 'en') {
            $q->columns(array('title'            => 'article.en_title',
                              'overview'         => 'article.en_overview'));
        } else {
            $q->columns(array('title'            => 'article.title',
                              'overview'         => 'article.overview'));
        }
        return $q;
    }

    public function listSearchCruise($keyword) {
        $q = $this->_db
                    ->select()
                    ->from('cruise', null)
                    ->join('cruise_category', 'cruise_category.id = cruise.cruise_category_id', null)
                    ->join('cruise_type', 'cruise_type.id = cruise.cruise_type_id', null)
                    ->join('article', 'article.reference_id = cruise.id', null)
                    ->where('cruise.status = ?', 1)
                    ->where('article.reference_type = ?', M_DbTable_Article::REFERENCE_TYPE_CRUISE)
                    ->where('article.title LIKE "%'. $keyword .'%" OR article.en_title LIKE "%'. $keyword .'%"')
                    ->columns(array('id' 			=> 'cruise.id',
                                    'category_id'	=> 'cruise_category.id',
                                    'thumbnail'		=> 'article.thumbnail'));

        if($this->getLang() == 'en') {
            $q->columns(array('title'       => 'cruise.en_name',
                              'overview'	=> 'article.en_overview'));
        } else {
            $q->columns(array('title'      	=> 'cruise.name',
                              'overview'	=> 'article.overview'));
        }
        return $q;
    }

}